package com.da.lon.wang.xlg.imgupload;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.DisplayImageOptions.Builder;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class ImageListAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private String path;
    private Context context;
    private List<ImageFloder> mImageFloders;
    private DisplayImageOptions options;

    public ImageListAdapter(Context context, String path,
                            List<ImageFloder> mImageFloders) {
        inflater = LayoutInflater.from(context);
        this.path = path;
        this.context = context;
        this.mImageFloders = mImageFloders;

        Builder build = new DisplayImageOptions.Builder();
        build.cacheInMemory();
        build.bitmapConfig(Bitmap.Config.RGB_565);
        build.showImageForEmptyUri(R.drawable.xlg_picture_default);
        build.showImageOnFail(R.drawable.xlg_picture_default);
        options = build.build();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return null != mImageFloders ? mImageFloders.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mImageFloders != null ? mImageFloders.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            ViewHoler holer = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.xlg_image_list_item, null);
                holer = new ViewHoler();
                holer.imageView = (ImageView) convertView.findViewById(R.id.image);
                holer.textView = (TextView) convertView.findViewById(R.id.text);
                convertView.setTag(holer);
            } else {
                holer = (ViewHoler) convertView.getTag();
            }

            ImageFloder imageFloder = mImageFloders.get(position);

            // BitmapUtils bitmapUtils = new BitmapUtils(context);
            // bitmapUtils.display(holer.imageView,imageFloder.firstImagePath,config);
            ImageLoader.getInstance().displayImage(
                    "file://" + imageFloder.firstImagePath, holer.imageView,
                    options);
            holer.textView.setText(imageFloder.dir.substring(imageFloder.dir
                    .lastIndexOf("/") + 1) + " (" + imageFloder.count + ")");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    private class ViewHoler {
        ImageView imageView;
        TextView textView;
    }

}
